package com.catmiw.util.dbf;

public class DBFTestCase {
	/**
	 * 读DBF文件
	 */
	private static void readDBF() {
		try {
			System.out.println("正在读取文件！");

			DBFReader dbfreader = new DBFReader("D:\\new_zszq\\T0002\\hq_cache\\base.dbf");
			for (int b = 0; b < dbfreader.getFieldCount(); b++) {
				if (b > 0)
					System.out.print(",");
				System.out.print(dbfreader.getField(b).getName().trim()
						.toUpperCase());

				if (b == (dbfreader.getFieldCount() - 1))
					System.out.print("\n");

			}
			for (int i = 0; dbfreader.hasNextRecord(); i++) {
				String aobj[] = dbfreader.nextRecordString();
				for (int b = 0; b < dbfreader.getFieldCount(); b++) {
					if (b > 0)
						System.out.print(",");
					System.out.print(aobj[b].trim());

					if (b == (dbfreader.getFieldCount() - 1))
						System.out.print("\n");

				}
			}
			System.out.println("读取文件成功！");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 写DBF文件
	 */
	private static void writeDBF() {

		try {
			DBFField aDBFField[] = new DBFField[4];
			aDBFField[0] = new DBFField("字段1", 'C', 20, 0);
			aDBFField[1] = new DBFField("字段2", 'C', 20, 0);
			aDBFField[2] = new DBFField("filed3", 'C', 20, 0);
			aDBFField[3] = new DBFField("filed4", 'C', 20, 0);

			System.out.println("正在生成DBF字段！");

			DBFWriter dbfwriter = new DBFWriter("d:/aa.dbf", aDBFField); /*生成 DBF 文件*/
			Object aobj[] = new Object[4];
			aobj[0] = "1";
			aobj[1] = "1";
			aobj[2] = "1";
			aobj[3] = "1";

			System.out.println("正在写入数据！");

			dbfwriter.addRecord(aobj);
			dbfwriter.close();

			System.out.println("执行成功！");
		} catch (DBFException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		//writeDBF();
		readDBF();
	}

}
